package com.imooc.springboot.dubbo.provider.transaction;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.springboot.dubbo.ServiceApi;
import org.mengyun.tcctransaction.api.Compensable;
import org.mengyun.tcctransaction.dubbo.context.DubboTransactionContextEditor;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = ServiceApi.class)
public class TransactionServiceImpl implements ServiceApi {
    @Override
    @Compensable(confirmMethod = "confirmSendMessage",cancelMethod = "cancelSendMessage",transactionContextEditor = DubboTransactionContextEditor.class)
    public String sendMessage(String message) {
        System.out.println("this is sendMessage try message = "+message);
        if (message.equals("123")){
            throw  new  NullPointerException();
        }
        return "transaction-provider-massage = "+message;
    }
    public String confirmSendMessage(String message) {
        System.out.println("this is confirmSendMessage try message = "+message);

        return "confirmSendMessage-provider-massage = "+message;
    }

    public String cancelSendMessage(String message) {
        System.out.println("this is cancelSendMessage try message = "+message);

        return "cancelSendMessage-provider-massage = "+message;
    }

    /**
     * 背景: 订单业务分布式事务改造
     * 业务:
     * 1. 判断传入的座位是否存在
     * 2. 查询过往订单,判断座位是否已售
     * 3. 新增订单
     * <p>
     * 逻辑:
     * 1. 新增一条订单
     * 2. 判断作为是否存在 & 是否已售
     * 3. 任意一条为假,则修改订单状态为无效状态
     *
     * @param seats
     */
    @Override
    @Compensable(confirmMethod = "confirmIsTrueSeats",cancelMethod = "cancelIsTrueSeats",transactionContextEditor = DubboTransactionContextEditor.class)
    public boolean isTrueSeats(String seats) {

        if (seats.equals("1,2,3")){
            throw new IllegalArgumentException();
        }else {
            return true;
        }

    }

    @Override
    @Compensable(confirmMethod = "confirmIsNotSold",cancelMethod = "cancelIsNotSold",transactionContextEditor = DubboTransactionContextEditor.class)
    public boolean isNotSold(String seats) {
        if (seats.equals("4,5")){
            throw new IllegalArgumentException();
        }else {
            return true;
        }
    }

    @Override
    @Compensable(confirmMethod = "confirmSaveOrder",cancelMethod = "cancelSaveOrder",transactionContextEditor = DubboTransactionContextEditor.class)
    public String saveOrder(String fieldId, String seats, String seatsName) {
        System.out.println("创建一个待支付的订单");
        return "";
    }

    public String confirmSaveOrder(String fieldId, String seats, String seatsName) {
        System.out.println("将订单修改为支付中");
        return "";
    }

    public String cancelSaveOrder(String fieldId, String seats, String seatsName) {
        System.out.println("将订单修改为已关闭");

        return "";
    }


    public boolean confirmIsTrueSeats(String seats) {
        System.out.println("this is confirmIsTrueSeats");
      return true;

    }

    public boolean cancelIsTrueSeats(String seats) {
        System.out.println("this is cancelIsTrueSeats");

        return true;


    }

    public boolean confirmIsNotSold(String seats) {
        System.out.println("this is confirmIsNotSold");

        return true;
    }

    public boolean cancelIsNotSold(String seats) {
        System.out.println("this is cancelIsNotSold");

        return true;
    }




}
